package com.jboltai.util.other;

import cn.hutool.core.util.StrUtil;
import com.jboltai.util.http.HttpUtil;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataSet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

/**
 * html工具类
 */
public class HtmlUtil {


    /**
     * 根据网页地址，加载文档
     * @param url
     * @return
     */
    public static Document loadDocument(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            return doc;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取网页代码
     * @param url
     * @return
     */
    public static String getHtmlByUrl(String url) {
        try {
            return HttpUtil.get(url).send().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取网页上指定选择器对应元素的html代码
     * @param url
     * @param selectors
     * @return
     */
    public static String getHtmlByUrl(String url, String selectors) {
        Document document = loadDocument(url);
        Elements elements = document.select(selectors);
        StringBuilder html = new StringBuilder();
        for (Element element : elements) {
            html.append(element.outerHtml());
        }
        return html.toString();

    }

    /**
     * 获取网页上指定选择器对应元素的html代码
     * @param document  完整的页面代码
     * @param selectors
     * @return
     */
    public static String extractHtml(Document document, String selectors) {
        Elements elements = document.select(selectors);
        //找不到 返回null
        if(elements.isEmpty()){
            return null;
        }
        //找到一个直接返回
        if(elements.size() == 1){
            return elements.get(0).outerHtml();
        }
        //找到多个 遍历拼接
        StringBuilder html = new StringBuilder();
        for (Element element : elements) {
            html.append(element.outerHtml());
        }
        return html.toString();
    }
    /**
     * 获取网页上指定选择器对应元素的html代码
     * @param document 完整的页面代码
     * @param selectors
     * @return
     */
    public static String extractText(Document document, String selectors) {
        Elements elements = document.select(selectors);
        //找不到 返回null
        if(elements.isEmpty()){
            return null;
        }
        //找到一个直接返回
        if(elements.size() == 1){
            return elements.get(0).text();
        }
        //找到多个 遍历拼接
        StringBuilder text = new StringBuilder();
        for (Element element : elements) {
            text.append(element.text());
            if (element.isBlock()) {
                text.append("\n");
            }
        }
        return text.toString();
    }


    /**
     * html转markdown
     * @param html
     * @return
     */
    public static String html2Markdown(String html) {
        if(StrUtil.isBlank(html)){
            return null;
        }
        return FlexmarkHtmlConverter.builder().build().convert(html);
    }

    /**
     * markdown转html
     * @param markdown
     * @return
     */
    public static String markdown2Html(String markdown) {
        if(StrUtil.isBlank(markdown)){
            return null;
        }
        MutableDataSet options = new MutableDataSet();
        Parser parser = Parser.builder(options).build();
        HtmlRenderer renderer = HtmlRenderer.builder(options).build();
        Node document = parser.parse(markdown);
        return renderer.render(document);
    }



}
